package com.hotmocha.jclient.net;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import com.hotmocha.jclient.ActionsManager;
import com.hotmocha.jclient.model.net.BaseMessage;
import com.hotmocha.jclient.model.net.TkMessage;

public class MessageReceiveHandler extends IoHandlerAdapter {
    private ActionsManager actionsManager;

    public MessageReceiveHandler(ActionsManager actionsManager) {
        this.actionsManager = actionsManager;
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        actionsManager.connected();
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        TkMessage tkMessage = (TkMessage)message;
        BaseMessage baseMessage = tkMessage.toModelMessage();
        this.actionsManager.receiveMessage(baseMessage);
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        actionsManager.disconnected();
    }

}
